package com.ms.demo;

import java.util.Hashtable;

/**
 * @description: 创建一个类，模拟从数据库获取实体类，并把它们存储在一个 Hashtable 中
 * @author: ms
 * @create: 2022-10-24 10:44
 */
public class ShapeCache {

    private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();

    //返回HashTable中元素的克隆类
    public static Shape getShape(String shapeId) {
        Shape cachedShape = shapeMap.get(shapeId);
        return (Shape) cachedShape.clone();
    }

    //模拟从数据库中查询数据 并保存到HashTable里去
    public static void loadCache() {
        Circle circle = new Circle();
        circle.setId("1");
        shapeMap.put(circle.getId(),circle);

        Square square = new Square();
        square.setId("2");
        shapeMap.put(square.getId(),square);

        Rectangle rectangle = new Rectangle();
        rectangle.setId("3");
        shapeMap.put(rectangle.getId(),rectangle);
    }
}